package com.teradata.webmvc.controller;

import net.bytebuddy.utility.RandomString;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "sleep", urlPatterns = "/sleep/*")
public class SleepServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest req,final HttpServletResponse resp) throws ServletException, IOException {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                    resp.getWriter().write("hello" + RandomString.make() + " H:" + req.getLocalAddr() + " IP:" + req.getRemoteAddr());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
